﻿using System;
using Microsoft.Win32;

namespace DasherBci
{
    public static class DasherSettings
    {
        private const string DasherRegistryKey = "HKEY_CURRENT_USER\\Software\\Inference Group\\Dasher3";
        private const string DasherRegistrySubKey = "Software\\Inference Group\\Dasher3";
        private const int DasherCoordinateDivisor = 1000;

        private static object GetDasherSettingValue(string settingName)
        {
            object value = Registry.GetValue(DasherRegistryKey, settingName, "");
            if (value == null)
            {
                throw new DasherBciException(ErrorCode.DasherRegistryKeyNotFound);
            }
            return value;
        }

        public static bool DasherRegistryKeyFound
        {
            get
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(DasherRegistrySubKey);
                return key != null;
            }
        }

        public static bool SocketModeEnabled
        {
            get { return (int) GetDasherSettingValue("SocketInputEnable") == 1; }
        }

        public static int SocketModePort
        {
            get 
            { 
                object value = GetDasherSettingValue("SocketPort"); 
                if(value is string)
                {
                    throw new DasherBciException(ErrorCode.SocketModeNotEnabled);
                }
                return (int)value;
            }
        }

        public static string XCoordinateLabel
        {
            get { return (string) GetDasherSettingValue("SocketInputXLabel"); }
        }

        public static string YCoordinateLabel
        {
            get { return (string) GetDasherSettingValue("SocketInputYLabel"); }
        }

        public static double XCoordinateValueMaximum
        {
            get { return Math.Abs((int) GetDasherSettingValue("SocketInputXMaxTimes1000")/DasherCoordinateDivisor); }
        }

        public static double XCoordinateValueMinimum
        {
            get { return Math.Abs((int) GetDasherSettingValue("SocketInputXMinTimes1000")/DasherCoordinateDivisor); }
        }

        public static double YCoordinateValueMaximum
        {
            get { return Math.Abs((int) GetDasherSettingValue("SocketInputYMaxTimes1000")/DasherCoordinateDivisor); }
        }

        public static double YCoordinateValueMinimum
        {
            get { return Math.Abs((int) GetDasherSettingValue("SocketInputYMinTimes1000")/DasherCoordinateDivisor); }
        }
    }
}
